<template>
  <div class="app-container">
    添加教师
    <el-form ref="teacher" :model="teacher" label-width="80px">
      <el-form-item label="教师姓名">
        <el-input v-model="teacher.teacherName" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="teacher.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="教师昵称">
        <el-input v-model="teacher.nickName" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="teacher.tel" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="teacher.email" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="工作地点">
        <el-input v-model="teacher.民族" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="教师职称">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-select v-model="teacher.rank" placeholder="请选择职称">
              <el-option
                v-for="item in rankList"
                :key="item.dictName"
                :label="item.dictName"
                :value="item.dictName"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="学院">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-select v-model="teacher.collegeName" placeholder="请选择学院">
              <el-option
                v-for="item in CollegeList"
                :key="item.collegeId"
                :label="item.collegeName"
                :value="item.collegeId"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="教师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="'http://localhost:8080/eduoss/fileoss/upload'"
          class="avatar-uploader"
        >
          <img :src="teacher.headshotUrl" style="width: 100px; height: 100px" />
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveTeacher()">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import dictApi from "@/api/dict";
import collegeApi from "@/api/college";
import Tinymce from "@/components/Tinymce"; //引入组件
export default {
  components: { Tinymce },
  data() {
    return {
      teacher: {
        sex: "男",
        headshotUrl:
          "https://zhanghao-edu.oss-cn-beijing.aliyuncs.com/2021/09/22/c8aaed56-3d9d-4c67-8ead-36089c32f735zh.jpg",
      },
      rankList: [],
      CollegeList: [],
    };
  },
  methods: {
    getDictList() {
      dictApi.getRankList().then((res) => {
        this.rankList = res.data.data;
      });
    },
    getCollege() {
      collegeApi.getCollegeList().then((res) => {
        this.CollegeList = res.data.page.list;
      });
    },
    //上传封面成功调用的方法
    handleAvatarSuccess(res, file) {
      this.teacher.avater = res.data.url;
    },
    //上传之前调用的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.getDictList();
    this.getCollege();
  },
};
</script>

<style>
</style>